// pages/courseDetail/courseDetail.js
Page({

  /**
   * Page initial data
   */
  data: {
    isCollection:false,
    courseDetailsComponent: 'detail',
    course: '',
    courseVideos: [],
    teacherInfo: '',
    courseMark: [],
    communication: '',
    isByCourse: true,
 
  },

  showComponent(event) {
    const component = event.currentTarget.dataset.component;
    console.log(component);
    this.setData({
      courseDetailsComponent: component
    });



  },
  //课程详情
  getCourseConcent(id) {
    const that = this;
    wx.request({
      url: `http://localhost:8080/course/details?id=${id}`,
      success(res) {
        const newcourse = res.data.data;
        console.log("1231231");
        console.log(newcourse);
        that.setData({
          course: newcourse,
        })
        that.byCourse(that.data.course.id, 1)
      }
    })
  },
  //课程列表
  getCourselist(id) {
    const that = this;
    wx.request({
      url: `http://localhost:8080/CourseVideo/allById?id=${id}`,
      success(res) {
        const newcourse = res.data.data;
        // if (!that.data.isByCourse) {
        //   that.setData({
        //     userInfo:"立即观看",
        //   })
        // }
        // console.log(newcourse);
        that.setData({
          courseVideos: newcourse,
        })
      }
    })
   

  },
  //名师介绍
  getCourseTeacher(id) {
    const that = this;
    wx.request({
      url: `http://localhost:8080/api/teacher/info?courseId=${id}`,
      success(res) {
        const newcourse = res.data.teacherInfo;
        console.log(newcourse);
        that.setData({
          teacherInfo: newcourse,
        })
      }
    })
    console.log(id);
  },

  //课堂交流
  getCourseCommunciation(id) {
    const that = this;
    wx.request({
      url: `http://localhost:8080/courseproblem/communication?id=${id}`,
      success(res) {
        const newcourse = res.data.data;
        console.log(newcourse);
        that.setData({
          communication: newcourse,
        })
      }
    })
  },



  //课程评价
  getCourseMark(id) {
    const that = this;
    wx.request({
      url: `http://localhost:8080/courseMark/evaluation?id=${id}`,
      success(res) {
        const newcourse = res.data.data;
        // console.log(newcourse);
        that.setData({
          courseMark: newcourse,
        })
      }
    })
  },
  //购买课程
  buyCourse() {
    wx.showToast({
      title: '购买成功',
      icon: 'success',
      duration: 2000 // 弹框显示2秒
    });
    wx.request({
      url: `http://localhost:8080/order/money`,
      method: "POST",
      header: {
        'content-type': 'application/json'
      },
      data: {
        courseId: this.data.course.id,
        userId: 1,
        payMoney: this.data.course.courseprice,
      },
    })
    this.setData({
      isByCourse: false,
    })
  },

  //查看是否购买
  byCourse(courseId, userId) {
    const that = this;
    wx.request({
      url: `http://localhost:8080/order/selectCourse?courseId=${courseId}&userId=${userId}`,
      success(res) {
        const newcourse = res.data.data;
        console.log(newcourse);
        if (newcourse != null) {
          that.setData({
            isByCourse: false,
          })
        }
      }
    })
  },

  //视频跳转
  goVideo(){
    const viedo=this.data.course.coursevideo;
    console.log("发送视频路径");
    console.log(viedo);
    wx.navigateTo({
      url: `/pages/course/courseVideo/courseVideo?src=${encodeURIComponent(viedo)}`,
    })
  },
  /**
   * Lifecycle function--Called when page load
   */
  onLoad(options) {


    const {
      id
    } = options;
    this.getCourseConcent(id);
    this.getCourselist(id);
    this.getCourseTeacher(id);
    this.getCourseMark(id);
    this.getCourseCommunciation(id);


  },

  /**
   * Lifecycle function--Called when page is initially rendered
   */
  onReady() {

  },

  /**
   * Lifecycle function--Called when page show
   */
  onShow() {

  },

  /**
   * Lifecycle function--Called when page hide
   */
  onHide() {

  },

  /**
   * Lifecycle function--Called when page unload
   */
  onUnload() {

  },

  /**
   * Page event handler function--Called when user drop down
   */
  onPullDownRefresh() {

  },

  /**
   * Called when page reach bottom
   */
  onReachBottom() {

  },

  /**
   * Called when user click on the top right corner to share
   */
  onShareAppMessage() {

  }
})